package com.fare.taluo.application.impl.crawl.crawlstatistics;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.fare.taluo.application.crawl.crawlstatistics.PersistentRecordeApplication;
import com.fare.taluo.core.domain.crawl.PersistentRecordeEntity;
import com.fare.taluo.util.DateUtils;
import com.fare.taluo.vo.crawl.statistics.PersistentRecordeVO;
@Transactional
@Service("persistentRecordeApplication")
@Scope
public class PersistentRecordeApplicationImpl implements PersistentRecordeApplication{

	@Override
	public void saveOrUpdate(PersistentRecordeVO vo) {
		PersistentRecordeEntity entity =new PersistentRecordeEntity();
		entity.setCreateTime(vo.getCreateTime());
		entity.setEndTime(vo.getEndTime());
		entity.setFinishedGrabDate(vo.getThisGrabDate());
		entity.setId(vo.getId());
		entity.setNextDoGrabDate(vo.getNextGrabDate());
		entity.setNextTime(vo.getNextTime());
		entity.setStartTime(vo.getStartTime());
		entity.setTaskLog(vo.getTaskLog());
		entity.setFrequency(vo.getFrequency());
		entity.setStatus(vo.getStatus());
		
		entity.saveOrUpdateUserCareLineEntity(entity);
	}

	@Override
	public void delete(Long id) {
		PersistentRecordeEntity entity =new PersistentRecordeEntity();
		entity.setId(id);
		entity.remove();
		
	}

	@Override
	public List<PersistentRecordeVO> findAll() {
		PersistentRecordeEntity tp =new PersistentRecordeEntity();
		List<PersistentRecordeVO> listVo=new ArrayList<PersistentRecordeVO>();
		List<PersistentRecordeEntity> listEntity=tp.findAll();
		if(listEntity.size()>0){
			listVo=imporfoVO(listEntity);
		}
		return listVo;
	}
	
	@Override
	public void updateVo(PersistentRecordeVO vo) {
		PersistentRecordeEntity entity =new PersistentRecordeEntity().get(vo.getId());
		entity.setCreateTime(vo.getCreateTime());
		entity.setEndTime(vo.getEndTime());
		entity.setFinishedGrabDate(vo.getThisGrabDate());
//		entity.setId(vo.getId());
		entity.setNextDoGrabDate(vo.getNextGrabDate());
		entity.setNextTime(vo.getNextTime());
		entity.setStartTime(vo.getStartTime());
		entity.setTaskLog(vo.getTaskLog());
		entity.setFrequency(vo.getFrequency());
		entity.setStatus(vo.getStatus());
		if(entity.getId() >0 && entity.getId() !=null){
			entity.update();
		}
		
	}

	//准备代替findAll（）
	@Override
	public List<PersistentRecordeVO> findByCondition(int pageSize, int pageNum) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<PersistentRecordeVO> findByGrabDate(String grabDateStr) {
		PersistentRecordeEntity tp =new PersistentRecordeEntity();
		List<PersistentRecordeVO> listVo=new ArrayList<PersistentRecordeVO>();
		Date grabDate=null;
		if(grabDateStr !=null ){
			grabDate=DateUtils.StringToDate("yyyy-MM-dd", grabDateStr);
		}
		List<PersistentRecordeEntity> listEntity=tp.findByGrabDate(grabDate);
		if(listEntity.size()>0){
			listVo=imporfoVO(listEntity);
		}
		return listVo;
		
	}
	
	public List<PersistentRecordeVO> imporfoVO(List<PersistentRecordeEntity> listEntity){
		List<PersistentRecordeVO> listVo=new ArrayList<PersistentRecordeVO>();
		for(PersistentRecordeEntity entity:listEntity){
			PersistentRecordeVO vo=new PersistentRecordeVO();
			vo.setCreateTime(entity.getCreateTime());
			vo.setEndTime(entity.getEndTime());
			vo.setEndTimeStr(entity.getEndTime() !=null?DateUtils.dateToStr(entity.getEndTime(), "yyyy-MM-dd HH:hh:ss"):null);
			vo.setStartTime(entity.getStartTime());
			vo.setStartTimeStr(entity.getStartTime() !=null?DateUtils.dateToStr(entity.getStartTime(), "yyyy-MM-dd HH:hh:ss"):null);
			vo.setId(entity.getId());
			vo.setNextGrabDate(entity.getNextDoGrabDate());
			vo.setNextGrabDateStr(entity.getNextDoGrabDate() !=null?DateUtils.dateToStr(entity.getNextDoGrabDate(), "yyyy-MM-dd"):null);
			vo.setNextTime(entity.getNextTime());
			vo.setNextTimeStr(entity.getNextTime() !=null?DateUtils.dateToStr(entity.getNextTime(), "yyyy-MM-dd HH:hh:ss"):null);
			vo.setTaskLog(entity.getTaskLog());
			vo.setThisGrabDate(entity.getFinishedGrabDate());
			vo.setThisGrabDateStr(entity.getFinishedGrabDate() !=null ?DateUtils.dateToStr(entity.getFinishedGrabDate(), "yyyy-MM-dd"):null);
			vo.setFrequency(entity.getFrequency()==null?0:entity.getFrequency());
			vo.setStatus(entity.getStatus());
			listVo.add(vo);
		}
		return listVo;
	}

}
